package RobotBattleArena;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Random;

/**
 * Created by Maxim on 14.02.2015.
 */
public class Main {
    public static void main(String[] args) throws Exception{
        BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Welcome to the RoboBattle Arena!!!\n"+
        "\nIn our battles take parts the following types of robots:\n"+
        "Heavy  (\"ALPHA\") - big brutal robots, have a lot of health, massive damage and strong defense.\n" +
        "Medium (\"BETA\")  - balanced amount of health, damage and defense.\n" +
        "Light  (\"GAMMA\") - small but nimble robots, which have small parameters.\n" +
        "\n RULES of RoboBattle Arena:\n" +
        "\n - battle consists of rounds;" +
        "\n - each round robots attacking each other for one time;" +
        "\n - order of attack and attack target selected randomly;" +
        "\n - when one robot attack another, he can dodge attack and take no damage;" +
        "\n - if attack was successful, attacked robot take damage depends on his defense and power of attacking;" +
        "\n - damage can not be less than 1;" +
        "\n - if robot health reached 0 and less, he is destroyed and out of battle;" +
        "\n - battle is conducted until there is left one robot.\n");

        int robotQuantity = definitionOfRobotQuantity(buffer); // amount of robots, which will fights in arena

        Random random = new Random();
        int robotType = 0; // type of created robot: Light(1-3), Medium(4-5), Heavy(6)
        ArrayList robots = new ArrayList(); // array which contains undestroyed robots and used to chose target
        ArrayList attackingRobots = new ArrayList(); // held robots that did not attack in this round

        // initialization of the required number of robots random type
        for (int i = 0; i < robotQuantity; i++) {

            robotType = random.nextInt(6);

            //System.out.println("Robot type: " + robotType);

            if (robotType > -1 && robotType <3) {
                robots.add(new LightRobot());
            }else if(robotType > 2 && robotType <5) {
                robots.add(new MediumRobot());
            }else {
                robots.add(new HeavyRobot());
            }

        }

        // representation of created robots
        for (int i = 0; i < robots.size(); i++) {
            System.out.println((i+1) + ") " +robots.get(i));
        }

        int roundNumber = 1;
        String sIn;
        int attackNumber = 0; // is used to determine the attacking robot
        AbstractRobot attackingRobot; // held robot which perform current attack

        int targetNumber = 0; // is used to determine the target of attacking robot
        AbstractRobot targetRobot; // robot that is the target of attack

        int remainHealth = 0; // if reached >0, robot is destroyed and out of battle
        do {

            System.out.println("\n ROUND " + roundNumber++ + "\n");
            attackingRobots.clear();

            // fill out the list of robots
            for (int i = 0; i < robotQuantity; i++) {
                attackingRobots.add(robots.get(i));
            }

            for (int i = 0; i < robotQuantity && robotQuantity > 1; i++) {

               // Thread.sleep(500);
                attackNumber = random.nextInt(attackingRobots.size());

                while(true) {
                    targetNumber = random.nextInt(robotQuantity);
                    if (attackingRobots.get(attackNumber) != robots.get(targetNumber)){
                        break;
                    }
                }

                attackingRobot = (AbstractRobot) attackingRobots.get(attackNumber);
                targetRobot = (AbstractRobot)robots.get(targetNumber);

                attackingRobot.attack(targetRobot);

                attackingRobots.remove(attackingRobot);

                remainHealth = targetRobot.getHealth();
                if (remainHealth <= 0){

                    System.out.println("\n" + targetRobot.getName() + " - DESTROYED!!!\n");

                    robots.remove(targetRobot);
                    attackingRobots.remove(targetRobot);

                    --robotQuantity;
                }

            }
            System.out.print("\nPress \"Enter\" key");
            sIn = buffer.readLine();
        } while (robots.size() > 1);

        AbstractRobot winner = (AbstractRobot)robots.get(0);

        System.out.println("\n" + winner.getName() + " win the battle!!!");


    }

    // This method define amount of robots, which will fights in arena
    private static int definitionOfRobotQuantity(BufferedReader buffer) throws NumberFormatException{
        System.out.println("Please, enter number of robots, which you want bring to the arena(Min - 2).");

        String sIn; //variables for reading user input coordinates
        int robotQuantity = 0;

        while (true) {
            try {
                sIn = buffer.readLine();

                robotQuantity = Integer.parseInt(sIn);
                if (robotQuantity > 1) {
                    return robotQuantity;
                }else {
                    System.out.println("Wrong value! Please input number(min - 2). ");
                }
            }catch (NumberFormatException exep){
                System.out.println("Wrong value! Please input number(min - 2). ");
            }catch(Exception exep){
                System.out.println("Something gone wrong! Please input number(min - 2). ");
            }
        }
    }
}
